/*
 * Copyright (c) 2019, XXX All Rights Reserved.
 */
package net.sf.log4jdbc.agent.driver;

import java.util.concurrent.ConcurrentHashMap;

import javassist.ClassPool;
import javassist.LoaderClassPath;

/**
 * <p>二进制代码增强器的抽象基类</p>
 * @author zhanxb
 * @since 2021/9/23
 **/
public abstract class AbstractTransformer
{

    private static final String TRACE_PREFIX = "[TRACE] ";
    private static final String DEBUG_PREFIX = "[DEBUG] ";
    private static final String INFO_PREFIX = "[INFO] ";
    private static final String WARN_PREFIX = "[WARN] ";
    private static final String ERROR_PREFIX = "[ERROR] ";

    private static ConcurrentHashMap<ClassLoader, ClassPool> classPoolMap = new ConcurrentHashMap<ClassLoader, ClassPool>();

    public static ClassPool getClassPool(ClassLoader loader)
    {
        if (null == loader)
        {
            return ClassPool.getDefault();
        }
        if(classPoolMap.get(loader)!=null){
            return classPoolMap.get(loader);
        }else {
            ClassPool classPool = new ClassPool(true);
            classPool.appendClassPath(new LoaderClassPath(loader));
            classPoolMap.put(loader,classPool);
            return classPool;
        }
    }

    public static void debug(String msg)
    {
        System.out.println(DEBUG_PREFIX + msg);
    }

    public static void trace(String msg)
    {
        System.out.println(TRACE_PREFIX + msg);
    }

    public static void info(String msg)
    {
        System.out.println(INFO_PREFIX + msg);
    }

    public static void warn(String msg)
    {
        System.out.println(WARN_PREFIX + msg);
    }

    public static void error(String msg)
    {
        System.out.println(ERROR_PREFIX + msg);
    }
}
